//
//  XSSubContentView.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/8.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit
import SnapKit

class XSSubContentView: UIView {

    // 当前控件底部的约束
    var bottomConstaint: Constraint?
    
    var statusViewModel: XSStatusViewModel? {
        didSet{
            // 去设置label显示的内容
            contentLabel.text = statusViewModel?.retweetedText
            
            self.bottomConstaint?.uninstall()
            
            // 判断是否有图片
            if let urls = statusViewModel?.status?.retweeted_status?.pic_urls where urls.count > 0 {
                // 有图片
                // 0. 设置数据
                pictureView.picUrls = urls
                // 1. 显示
                pictureView.hidden = false
                // 2. 更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView).offset(XSStatusCellMargin).constraint
                })
                
            }else{
                // 1. 隐藏
                pictureView.hidden = true
                // 2. 更新约束 没有配图当前控件的底部就等于 文字内容的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(contentLabel).offset(XSStatusCellMargin).constraint
                })
            }

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   
    private func setupUI(){
        backgroundColor = UIColor(white: 239/255, alpha: 1)
        // 添加子控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(XSStatusCellMargin)
            make.trailing.equalTo(self).offset(-XSStatusCellMargin)
        }
        
        // 添加配图视图的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(XSStatusCellMargin)
        }

        // 约束转发微博的底部
        self.snp_makeConstraints { (make) -> Void in
             self.bottomConstaint = make.bottom.equalTo(pictureView).offset(XSStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载控件
    
    // 显示转发微博内容的视图
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        // 多行
        label.numberOfLines = 0
        label.text = "这是下级的转发内容"
        return label
    }()
    
    // 配图视图
    private lazy var pictureView: XSPictureView = XSPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    


    
}
